//alert('tf_tree.js');
/*
	var a = ['Root', '', 'dcsh'];
	var a_1 = ['b-1', 'url_b-1', 'dcsh'];
	var a_1_checkbox = ['b-1', 'url_b-1', 'dcsh'];
	var a_1_icon = ['b-1', 'images/btn_right.jpg', 'dcsh'];

	var pfx = 'a';
	var checkBoxName = 'defaultCheckBoxName';
	var expandToLevel = 0;
	var rootUrl = false;
	var selectAllCheckbox = false;
	
<div id="treeDiv" style="border:1px solid black; overflow:auto; width:500px; height:200px" >
	<script>document.write(buildTree(pfx, 'checkboxname1'));</script>
</div>
*/
	var checkBoxName = 'defaultCheckBoxName';
	var expandToLevel = 1;
	var pageSize = 50;
	var selectAllCheckbox = false;
	var rootUrl = false;
	var imgDir = '../images/tree/';
	var cbPfx = 'cb';
	
	function bTree(parentName, renderFlag, pageStartFrom){		
		var pfx = parentName.split('_')[0];
		var startFrom = (pageStartFrom == undefined) ? 1 : parseInt(pageStartFrom);
		var endTo = parseInt(startFrom) + parseInt(pageSize) -1;
		var i = startFrom; 
		var ts = '';
		var renderChild = (renderFlag == undefined || renderFlag == true	) ? true : renderFlag;
		
		while(checkNextObjExist(parentName + '_' + i) && (i >= startFrom && i <= endTo)){
			var aryName = parentName + '_' + i;		
			var urlAry = eval(aryName);
			var hasNext = checkNextObjExist(parentName + '_' + eval(i + 1));
			var hasChild = checkNextObjExist(parentName + '_' + i + '_1');
			var expandChild = (eval(aryName.split('_').length-1) >= expandToLevel) ? false : true;		
			var onOffCssName = 'img' + (hasNext ? 'T' : 'L') + (hasChild ? ( (expandChild && renderChild) ? 'minus' : 'plus' ) : '');
							//paging case
							onOffCssName = (i == endTo && hasNext) ? 'imgDown' : onOffCssName;
			var folderCssName = 'img' + (hasChild ? ( (expandChild && renderChild) ? 'Fopen' : 'Fclose' ) : 'File');
			ts = ts + '<table id="' + tn(aryName, 'tab') + '" >' +
					'<tr>' +
					'<td class="c1" ><img id="' + tn(aryName, 'onOff') + '" class="' + onOffCssName + '" src="' + imgDir + 'blank.png" onclick="onOffChild(this, \'' + pfx + '\' )" /></td>' +
					'<td class="c2" ><nobr>' ;
			ts = ts + buildIcon(aryName, folderCssName);
			ts = ts + buildCheckbox(aryName, checkBoxName);
			ts = ts + buildUrl(aryName);
			ts = ts + '</nobr></td>' +
					'</tr>' +
					//check whether have child
					'<tr class="dy' + ( (hasChild && expandChild && renderChild) ? 'On' : 'Off' ) + '" >' +
						'<td class="c3 img' + (hasNext ? 'I' : 'B') + '" ><img src="' + imgDir + 'blank.png" /></td>' +
						'<td class="c4" >' + 
					( (!expandChild || !renderChild) ? '' : bTree(parentName + '_' + i) ) + 
						'</td>' +
					'</tr>' +
					'</table><div id="' + tn(aryName, 'page') + '" ></div>' ;			
			i++;
		}
		return ts;
	}
	function tn(_arrayName, _toName){	//trans name ----- > a_1_1, to a_folder_1_1
		var _tStr = _arrayName.substr(0, _arrayName.indexOf('_')) + '_' + _toName + _arrayName.substr(_arrayName.indexOf('_'), _arrayName.length);
		return _tStr;
	}
	function buildIcon(_aryName, _folderCssName){
		var _iconStr = '';
		var _icon_title = '';
		var _icon_src = '';
		var pfx = _aryName.split('_')[0];
		if(checkNextObjExist(_aryName + '_icon')){
			var _iconAry = eval(_aryName + '_icon')	;			
			if(_iconAry[2] != undefined){
				var _icon_checked = (_iconAry[2].indexOf('c') != -1) ? true : false;
				var _icon_disabled = (_iconAry[2].indexOf('d') != -1) ? true : false;
				var _icon_selected = (_iconAry[2].indexOf('s') != -1) ? true : false;
				var _icon_hidden = (_iconAry[2].indexOf('h') != -1) ? true : false;
			}
			var _icon_title = ' title="' + _iconAry[0] + '" ';
			var _icon_src = ' style="background-image:url(' + _iconAry[1] + ')" ';
		}
		_iconStr = _iconStr + '<img id="' + tn(_aryName, 'folder') + '" class="' + _folderCssName + '" src="' + imgDir + 'blank.png" ' + _icon_title + _icon_src + ' />' ;
		//alert(_iconStr);
		return _iconStr;
	}
	function buildUrl(_aryName){								
		//---------------- URL (+) ---------------------------------------------------------------
		var _urlStr = '';
		var pfx = _aryName.split('_')[0];
			if(checkNextObjExist(_aryName)){
				var _urlAry = eval(_aryName)	;			
				if(_urlAry[2] != undefined){
					var _url_checked = (_urlAry[2].indexOf('c') != -1) ? true : false;
					var _url_disabled = (_urlAry[2].indexOf('d') != -1) ? true : false;
					var _url_selected = (_urlAry[2].indexOf('s') != -1) ? true : false;
					var _url_hidden = (_urlAry[2].indexOf('h') != -1) ? true : false;
				}
				_urlStr = _urlStr + '<font id="' + tn(_aryName, 'url') + '" class="' + ((_url_selected) ? 'urlHighLighted' : 'urlNormal') + '" >' ;	
				if(!_url_disabled){
					_urlStr = _urlStr +	'<a href="' + _urlAry[1] + '" ' + ((_url_disabled) ? 'disabled' : '') + ' >';
				}
				_urlStr = _urlStr +	_urlAry[0] ; 
				//tommy
				//_urlStr = _urlStr +	_aryName;
				if(!_url_disabled){
					_urlStr = _urlStr +	'</a>';
				}				
				_urlStr = _urlStr +	'</font>';
			}
		return _urlStr;
		//---------------- URL (-) ---------------------------------------------------------------
	}	
	function buildCheckbox(_aryName, _checkBoxName){
		//---------------- checkbox (+) ----------------------------------------------------------
		var _cbStr = '';
		var pfx = _aryName.split('_')[0];
		if(checkNextObjExist(_aryName + '_checkbox')){
			var _cbAry = eval(_aryName + '_checkbox')	;		
			if(_cbAry[0]!= undefined){
				var _cbAttr = _cbAry[0];				
				var _cb_checked = (_cbAttr.indexOf('c') != -1) ? true : false;
				var _cb_disabled = (_cbAttr.indexOf('d') != -1) ? true : false;
				var _cb_selected = (_cbAttr.indexOf('s') != -1) ? true : false;
				var _cb_hidden = (_cbAttr.indexOf('h') != -1) ? true : false;
			}
				_cbStr = '<input type="checkbox" ' +
//													'title="' + pfx + _aryName.replace(pfx, cbPfx) + '" ' +
//													'title="' + _checkBoxName + '" ' +
													'id="' + pfx + _aryName.replace(pfx, cbPfx) + '" ' +
													'name="' + _checkBoxName + '" ' +
													'onclick="_saChild(this); //cbOnClick(this, \'' + pfx + '\' )" ' +
														((_cb_disabled) ? ' disabled ' : '') +
														((_cb_checked) ? ' checked ' : '') +
													' />' ;
		}
		//---------------- checkbox (-) ----------------------------------------------------------		
		//alert(_cbStr);
		return _cbStr;
	}
	function checkNextObjExist(objId){
		var nextExist = false;
		try{
			eval(objId);
			nextExist = true;
		}catch(e){
			nextExist = false;
		}	
		return nextExist;
	}
	function onOffChild(thisObj, pfx){		
		var curId = thisObj.id.replace('_onOff', ''); // a_onOff_1_1_1 to a_1_1_1
		var parentId = curId.substring(0, curId.lastIndexOf('_'));
		var hasChild = checkNextObjExist(curId + '_1');
		var tableObj = document.getElementById(tn(curId, 'tab'));
		var thisExpanded = ( document.getElementById(tn(curId, 'folder')).className == 'imgFopen' ) ? true : false;
			//onOff icon
		if(thisObj.className.indexOf('minus') != -1 || thisObj.className.indexOf('plus') != -1 ){
			tableObj.rows[1].className = (thisObj.className.indexOf('minus') != -1) ? 'dyOff' : 'dyOn' ;
			if(thisObj.className.indexOf('plus') != -1){
				//render its child		
				if(tableObj.rows[1].cells[1].innerHTML == ''){
					tableObj.rows[1].cells[1].innerHTML = bTree(curId, false);
				}
			}				
			//onOff icon - construct className 'imgTplus', 'imgTminus', 'imgLplus', 'imgLminus'
			thisObj.className = thisObj.className.substr(0,4) + ((thisObj.className.indexOf('minus') != -1) ? 'plus' : 'minus' );
			//folder icon
			document.getElementById(thisObj.id.replace('onOff', 'folder')).className = (thisObj.className.indexOf('minus') != -1) ? 'imgFopen' : 'imgFclose' ;

			//checkbox behaviour
/*			
			var thisCheckbox = document.getElementById(thisObj.id.replace('onOff', cbPfx));
			var thisChecked = thisCheckbox.checked;
			if(thisChecked){
				cbOnClick(thisCheckbox);
			}				
*/			
		}
		//paging button - down arrow button
		if(thisObj.className.indexOf('Down') != -1){
			thisObj.className = 'img' + (hasChild ? ('T' + (thisExpanded ? 'minus' : 'plus')) : 'T');
			var startIndex = parseInt(curId.substring(curId.lastIndexOf('_')+1, curId.length)) + 1;
			//var endIndex = eval(parseInt(startIndex) + pageSize);
			document.getElementById(thisObj.id.replace('onOff', 'page')).innerHTML = bTree(parentId, false, startIndex);
			var parentCb = getObj(cbPfx, parentId);
			if(parentCb){
				if(parentCb.checked){				
					cbOnClick(parentCb);
				}
			}
		}			
	}
	function buildTree(_pfx, cbxName){
		checkBoxName = (cbxName != undefined) ? cbxName : checkBoxName;
		var treeStr = '';
		var rootAry = eval(_pfx);
		treeStr =	'<table class="tree" id="' + _pfx + '_tab" >' +		
		
												'		<tr class="dy' + (rootUrl ? 'On' : 'Off' ) + '" >' +
												'			<td class="c1" >' +
												'			</td>' +
												'			<td class="c2 urlNormal" >' ;
												if(selectAllCheckbox)
		treeStr =	treeStr + '				<input type="checkbox" id="' + _pfx + cbPfx + '" title="' + _pfx + cbPfx + '" name="' + checkBoxName + '" onclick="_saAll(this);//cbOnClick(this)" />' ;
		treeStr =	treeStr + 
												'			<a href="' + rootAry[1] + '" >' + rootAry[0] + 																								
												'			</a></td>' +
												'  </tr>' +												
												'		<tr>' +
												
												'			<td class="c3 imgB" ><img src="' + imgDir + 'blank.png" />' +
												'			</td>' +
												'			<td class="c4" >' + bTree(_pfx) + '</td>' +														
												'  </tr>' +
												'</table>';
		return treeStr; 
	}
	function expandAllAction(parentName){
		var i = 1;
		var pfx = parentName.split('_')[0]
		while(checkNextObjExist(parentName + '_' + i)){
			var curId = parentName + '_' + i;
			var needExpand = (getObj('onOff', curId).className.indexOf('minus') == -1) ? true : false;			
			if(needExpand){
				onOffChild(getObj('onOff', curId), pfx);
			var needExpandAgain = (getObj('onOff', curId).className.indexOf('minus') == -1) ? true : false;
			if(needExpandAgain){
				onOffChild(getObj('onOff', curId), pfx);
			}
		}
		expandAllAction(curId);
		i++;
		}	
	}
	function expandAll(pfx){
		expandAllAction(pfx);
	}
	function collapseAll(pfx){
		collapseToLevel(pfx, 1);
	}
	function collapseToLevel(parentName, targetLevel){
		targetLevel = parseInt(targetLevel);
		var curLevel = 0;
		var pfx = parentName.split('_')[0];
		if(parentName.indexOf('_') != -1){
			curLevel = parentName.split('_').length -1;
		}
		var withInLevel = (curLevel < targetLevel) ? true : false ;
		//alert(parentName + '*' + curLevel + '*' + withInLevel);
		if(!withInLevel){
			//the icon is exist.
			if(getObj('onOff', parentName) != undefined){
				//the icon is minus.
				if(getObj('onOff', parentName).className.indexOf('minus') != -1){
					//collaspe the its child.
					onOffChild(getObj('onOff', parentName), pfx);
				}
			}
		}
		var i = 1;
		while(checkNextObjExist(parentName + '_' + i) && withInLevel){
			var curId = parentName + '_' + i;
			collapseToLevel(curId, targetLevel);
			i++;
		}
	}
	function cbOnClick(thisObj, pfx){
		var curId = thisObj.id.replace(cbPfx, pfx);
		var hasChild = checkNextObjExist(curId + '_1');			
		var tableObj = document.getElementById(thisObj.id.replace(cbPfx, 'tab'));		
		if(hasChild){
			var cbCol = tableObj.rows[1].cells[1].getElementsByTagName('input');
			for(var i=0; i<cbCol.length; i++){
				if(!cbCol[i].disabled){
					cbCol[i].checked = thisObj.checked;
				}
			}
		}
		//resetSelectAll();
		var curLevel = curId.split('_').length - 1;
		var tmpParentId = curId;
		if(!thisObj.checked){
			//parentId = curId.substring(0, curId.lastIndexOf('_'));		
			for(var i=curLevel; i>0; i--){
				tmpParentId = tmpParentId.substring(0, tmpParentId.lastIndexOf('_'));
				getObj(cbPfx, tmpParentId).checked = false;
			}
		}
	}
	var expandFinish = false;
	function expandToChild(childName){
		expandFinish = false;
		var pfx = childName.split('_')[0];
		if(checkNextObjExist(childName)){
			expandToChildAction(pfx, childName);
		}else{
			alert('Child does not exist!');
		}
	}
	function expandToChildAction(parentName, childName){
		var i = 1;
		var belongTo = false;
		var pfx = parentName.split('_')[0];
		//alert(childName + '*' + parentName + '*' + belongTo);
		while(checkNextObjExist(parentName + '_' + i)&& !expandFinish){
			var curId = parentName + '_' + i;
			belongTo = (childName.indexOf(curId) != -1) ? true : false;					
			if(curId == childName){
				expandFinish = true;
				getObj('url', curId).className = 'urlHighLighted' ;
			}
			//alert(belongTo + '^^' + !expandFinish);
			if(belongTo && !expandFinish){
				//alert(childName + '*' + curId + '*' + belongTo);			
				var needExpand = (getObj('onOff', curId).className.indexOf('minus') == -1) ? true : false;			
				if(needExpand){
					onOffChild(getObj('onOff', curId), pfx);
				}
				var needExpandAgain = (getObj('onOff', curId).className.indexOf('minus') == -1) ? true : false;
				if(needExpandAgain){
					onOffChild(getObj('onOff', curId), pfx);
				}
				expandToChildAction(curId, childName);
			}else{
				//Down arrow case
				var needExpand = (getObj('onOff', curId).className.indexOf('imgDown') != -1) ? true : false;			
				if(needExpand){
					onOffChild(getObj('onOff', curId), pfx);
				}			
			}
			i++;					
		}	
	}
	function scrollToChild(childName){
		try{		
			expandToChild(childName);
			getObj('url', childName).scrollIntoView();
		}catch(e){			
			alert(e.description + '______' + 'at function scrollToChild');
		}
	}	
	function getObj(objName, aryId){
		try{
			if(document.getElementById(tn(aryId, objName))){
				return document.getElementById(tn(aryId, objName));
			}else{
				//alert(tn(aryId, objName));
				//alert('__a____' + 'at a function getObj');
			}
		}catch(e){			
			alert(e.description + '___b___' + 'at function getObj');
		}
	}
























//alert('selectAllCheckbox.js');
/*
	<label>
	<input class="inputCheckbox" type="checkbox" id="fieldName" name="fieldName" onclick="_saChild(this)" />
	All </label>
		<table border="1" cellspacing="2" cellpadding="3">
			<tr>
				<td>
					<label>
					<input class="inputCheckbox" type="checkbox" id="fieldName_1" name="fieldName_1" onclick="_saChild(this)" />
					option 1 </label>
				</td>
			</tr>									
		</table>
	<script>selectAllOnload('fieldName')</script>
*/

/* ----------- Select All Checkbox Colection (START) ----------------------------- */

var _sMode = true; // 'selectAll'; // check parent if all chlid are checked.
//var _sMode  = false;  //'selectedChild'; // check parent if more than one chlid is checked.

function _saAll(thisObj){
	var _fieldName = thisObj.id.split('_')[0];
	_checkChild(_fieldName, thisObj.checked);
}
function _saChild(thisObj){
	var _fieldName = thisObj.id;
	_checkChild(_fieldName, thisObj.checked);
	_uncheckParent(_fieldName);
}
function	_checkChild(_fieldName, _isChecked){
	var i = 1;
	while(document.getElementById(_fieldName + '_' + i) != undefined){
		document.getElementById(_fieldName + '_' + i).checked = _isChecked;
		_checkChild(_fieldName + '_' + i, _isChecked);
		i++;
	}
}
function	_uncheckParent(_fieldName){
	var _parentName = _fieldName.substr(0, _fieldName.lastIndexOf('_'));
	var i = 1;
	var _checkParent = _sMode;
	while(document.getElementById(_parentName + '_' + i) != undefined){
		if(document.getElementById(_parentName + '_' + i).checked == !_sMode){
			_checkParent = !_sMode;
			break;
		}
		i++;
	}
	if(document.getElementById(_parentName) != undefined){
		document.getElementById(_parentName).checked = _checkParent;
	}
	// check upper level.
	if(_parentName.indexOf('_') != -1){
		_uncheckParent(_parentName);
	}else{
		//document.getElementById(_parentName).checked = _checkParent;
	}
}


function selectAllOnload(_fieldName){
	//var _fieldName = document.getElementById(_fieldName).id;
	//_checkChild(_fieldName, thisObj.checked);
	//_uncheckParent(_fieldName);	
}

function _isAllChecked(_fieldName){
	var i = 1;
	//alert(_fieldName);
	while(document.getElementById(_fieldName + '_' + i) != undefined){		
		if(document.getElementById(_fieldName + '_' + i).checked == false){
			//alert(_fieldName + '_' + i + 'a');
			return false;
		}
		if(_isAllChecked(_fieldName + '_' + i) == false){
			return false;
		}
		i++;
	}
}


/* ----------- Select All Checkbox Colection (END) ----------------------------- */

























